package com.dmap.admin.service.backgroundv3.Impl;

import com.dmap.admin.controller.backgroundv3.param.AndroidVersionEditParam;
import com.dmap.admin.controller.backgroundv3.param.AndroidVersionParam;
import com.dmap.admin.controller.backgroundv3.param.AndroidVersionPublishParam;
import com.dmap.admin.dao.AndroidVersionMapper;
import com.dmap.admin.po.AndroidVersion;
import com.dmap.admin.po.AndroidVersionExample;
import com.dmap.admin.po.AppVersion;
import com.dmap.admin.service.backgroundv3.APPVersionService;
import com.dmap.admin.service.backgroundv3.AndroidVersionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author: zmh
 * @create: 18-8-22
 * @description: 安卓版本
 **/
@Service
public class AndroidVersionServiceImpl implements AndroidVersionService {
    @Resource
    private AndroidVersionMapper androidVersionMapper;
    @Resource
    private APPVersionService appVersionService;

    @Transactional
    @Override
    public int insertAndroidVersion(AndroidVersionParam androidVersionParam) {
        AndroidVersion androidVersion = new AndroidVersion();
        if (androidVersionParam.getUrl() != null) {
            androidVersion.setUrl(androidVersionParam.getUrl());
        }
        if (androidVersionParam.getStatus() != null) {
            androidVersion.setStatus(androidVersionParam.getStatus());
        }
        if (androidVersionParam.getNewVersionCode() != null) {
            androidVersion.setNewVersionCode(androidVersionParam.getNewVersionCode());
        }
        if (androidVersionParam.getNewVersion() != null) {
            androidVersion.setNewVersion(androidVersionParam.getNewVersion());
        }
        if (androidVersionParam.getMinVersionCode() != null) {
            androidVersion.setMinVersionCode(androidVersionParam.getMinVersionCode());
        }
        if (androidVersionParam.getMinVersion() != null) {
            androidVersion.setMinVersion(androidVersionParam.getMinVersion());
        }
        if (androidVersionParam.getMd5() != null) {
            androidVersion.setMd5(androidVersionParam.getMd5());
        }
        if (androidVersionParam.getJsonUrl() != null) {
            androidVersion.setJsonUrl(androidVersionParam.getJsonUrl());
        }
        if (androidVersionParam.getDescriptionEn() != null) {
            androidVersion.setDescriptionEn(androidVersionParam.getDescriptionEn());
        }
        if (androidVersionParam.getDescription() != null) {
            androidVersion.setDescription(androidVersionParam.getDescription());
        }
        if (androidVersionParam.getAppNumber() != null) {
            androidVersion.setAppNumber(androidVersionParam.getAppNumber());
        }
        androidVersion.setUpdateTime(System.currentTimeMillis()/1000);
        androidVersion.setCreateTime(System.currentTimeMillis()/1000);
        androidVersion.setUploadTime(System.currentTimeMillis()/1000);
        return androidVersionMapper.insertSelective(androidVersion);
    }
    @Transactional
    @Override
    public int updateAndroidVersion(AndroidVersionEditParam androidVersionEditParam) {
        AndroidVersion androidVersion = androidVersionMapper.selectByPrimaryKey(Long.parseLong(androidVersionEditParam.getAndroidVersionId()));
        if (androidVersion == null) {
            //没有该version
            return 2;
        }
        if (androidVersionEditParam.getUrl() != null) {
            androidVersion.setUrl(androidVersionEditParam.getUrl());
        }
        if (androidVersionEditParam.getStatus() != null) {
            androidVersion.setStatus(androidVersionEditParam.getStatus());
        }
        if (androidVersionEditParam.getNewVersionCode() != null) {
            androidVersion.setMinVersionCode(androidVersionEditParam.getNewVersionCode());
        }
        if (androidVersionEditParam.getNewVersion() != null) {
            androidVersion.setNewVersion(androidVersionEditParam.getNewVersion());
        }
        if (androidVersionEditParam.getMinVersionCode() != null) {
            androidVersion.setNewVersionCode(androidVersionEditParam.getMinVersionCode());
        }
        if (androidVersionEditParam.getMinVersion() != null) {
            androidVersion.setMinVersion(androidVersionEditParam.getMinVersion());
        }
        if (androidVersionEditParam.getMd5() != null) {
            androidVersion.setMd5(androidVersionEditParam.getMd5());
        }
        if (androidVersionEditParam.getJsonUrl() != null) {
            androidVersion.setJsonUrl(androidVersionEditParam.getJsonUrl());
        }
        if (androidVersionEditParam.getDescriptionEn() != null) {
            androidVersion.setDescriptionEn(androidVersionEditParam.getDescriptionEn());
        }
        if (androidVersionEditParam.getDescription() != null) {
            androidVersion.setDescription(androidVersionEditParam.getDescription());
        }
        if (androidVersionEditParam.getAppNumber() != null) {
            androidVersion.setAppNumber(androidVersionEditParam.getAppNumber());
        }
        androidVersion.setUpdateTime(System.currentTimeMillis()/1000);
        androidVersion.setUploadTime(System.currentTimeMillis()/1000);
        return androidVersionMapper.updateByPrimaryKeySelective(androidVersion);
    }

    @Override
    public int deleteAndroidVersion(long id) {
        AndroidVersion androidVersion = androidVersionMapper.selectByPrimaryKey(id);
        if (androidVersion == null) {
            return 0;
        }
        //判断是否有最新版本app关联
        AppVersion appVersion = appVersionService.getAppVersionByAppNumber(androidVersion.getAppNumber());
        if(appVersion != null && appVersion.getAndroidVersion().equals(androidVersion.getNewVersion())){
            androidVersion.setNewVersion("");
            appVersionService.updateAppAndroidVersion(androidVersion);
        }
        return  androidVersionMapper.deleteByPrimaryKey(id);
    }
    @Transactional
    @Override
    public int deleteAndroidVersionList(String ids) {
        List<String> androidVersionList = Arrays.asList(ids.split(","));
        int count = 0;
        for (String id : androidVersionList) {
            count += deleteAndroidVersion(Long.parseLong(id));
        }
        if (count == androidVersionList.size()) {
            return 1;
        }
        return 0;
    }
    @Transactional
    @Override
    public int updateAndroidVersionPublish(AndroidVersionPublishParam param) {
        AndroidVersion androidVersion = androidVersionMapper.selectByPrimaryKey(Long.parseLong(param.getAndroidVersionId()));
        if (androidVersion == null) {
            //没有该version
            return 2;
        }
        //1.代表正常  2代表禁用
        if(androidVersion.getStatus() == 1){
            androidVersion.setStatus(2);
        }else{
            androidVersion.setStatus(1);
            //给appVersion添加最新版本
            appVersionService.updateAppAndroidVersion(androidVersion);
        }
        androidVersion.setUpdateTime(System.currentTimeMillis()/1000);
        return androidVersionMapper.updateByPrimaryKeySelective(androidVersion);
    }

    @Override
    public List<AndroidVersion> selectAndroidVersion() {
        AndroidVersionExample androidVersionExample = new AndroidVersionExample();
        AndroidVersionExample.Criteria criteria = androidVersionExample.createCriteria();
        //增加条件
        return androidVersionMapper.selectByExample(androidVersionExample);
    }

    @Override
    public int countAndroidVersion() {
        AndroidVersionExample androidVersionExample = new AndroidVersionExample();
        AndroidVersionExample.Criteria criteria = androidVersionExample.createCriteria();
        //增加条件
        return androidVersionMapper.countByExample(androidVersionExample);
    }

    @Override
    public AndroidVersion getAndroidVersion(long id) {
        return androidVersionMapper.selectByPrimaryKey(id);
    }

    @Override
    public int deleteAndroidVersionByAppNumber(String appNumber) {
        AndroidVersionExample androidVersionExample = new AndroidVersionExample();
        AndroidVersionExample.Criteria criteria = androidVersionExample.createCriteria();
        criteria.andAppNumberEqualTo(appNumber);
        return androidVersionMapper.deleteByExample(androidVersionExample);
    }
}
